package net.daum.webtoon.android.jibx;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.jibx.runtime.BindingDirectory;
import org.jibx.runtime.IBindingFactory;
import org.jibx.runtime.IMarshallingContext;
import org.jibx.runtime.IUnmarshallingContext;
import org.jibx.runtime.JiBXException;

/**
 * The Class XMLBinder.
 */
public class XMLBinder {

	/**
	 * Marshalling.
	 * 
	 * @param out
	 *            the out
	 * @param cls
	 *            the cls
	 * @param obj
	 *            the obj
	 * @throws IOException
	 *             Signals that an I/O exception has occurred.
	 */
	public static void marshalling(OutputStream out, Class<?> cls, Object obj) throws IOException {
		try {
			IBindingFactory bf = BindingDirectory.getFactory(cls);
			IMarshallingContext mc = bf.createMarshallingContext();
			mc.setOutput(out, null);
			mc.marshalDocument(obj);
		} catch (JiBXException e) {
			throw new IOException(e.getMessage());
		}
	}

	/**
	 * Unmarshalling.
	 * 
	 * @param in
	 *            the in
	 * @param cls
	 *            the cls
	 * @return the object
	 * @throws IOException
	 *             Signals that an I/O exception has occurred.
	 */
	public static Object unmarshalling(InputStream in, Class<?> cls) throws IOException {
		try {
			IBindingFactory bf = BindingDirectory.getFactory(cls);
			IUnmarshallingContext umc = bf.createUnmarshallingContext();
			return umc.unmarshalDocument(in, null);
		} catch (JiBXException e) {
			e.printStackTrace();
			throw new IOException(e.getMessage());
		}
	}

}
